<?php
/*
 * @Author       : chaihao
 * @Date         : 2020-06-04 09:52:20
 * @FilePath     : /ai/Services/api/AnimationService.php
 */

namespace app\Services\api;

use app\models\PltfAnimation;
use app\models\PltfCard;
use app\models\Steaming;
use app\models\User;
use app\models\UserLimit;
use app\Services\ApiService;
use Yii;
use yii\db\Exception;

class AnimationService extends ApiService
{

    /**
     *  返回当前流地址
     */
    public function getSteaming($data)
    {
        $steamURL = Steaming::find()->where(['roomID' => $data['roomid']])
            ->select(['steamURL'])
            ->orderBy(['id' => SORT_DESC])
            ->asArray()
            ->scalar();
        if (empty($steamURL)) {
            return  ['code' => -1, 'message' => '未查找到数据'];
        }
        return  ['code' => 200, 'data' => $steamURL];
    }

    /**
     *  新增动作请求
     */
    public function saveAnimation($data)
    {
        $info = [
            'roomID'  =>  $data['roomid'],
            'row'  => $data['row'],
            'col'  => $data['col'],
            'status'  => $data['status'],
            'userID'  => $data['user_id'],
            'updatetime' => date('Y-m-d H:i:s', time()),
        ];
        try {
            Yii::$app->db->createCommand()->insert(PltfAnimation::tableName(), $info)->execute();
            return ['code' => 200, 'message' => '操作成功'];
        } catch (Exception $e) {
            return ['code' => -1, 'message' => $e->getMessage()];
        }
    }

    /**
     *  查询新的动作（返回比传参updatetime更新的所有动作信息）w
     */
    public function checkAnimation($data)
    {
        $result = PltfAnimation::find()->where(['roomID' => $data['roomid']])
            ->andWhere(['>', 'updatetime', $data['updatetime']])->asArray()->all();
        $content = '';
        if (count($result) >= 1) {
            for ($i = 0; $i < count($result); $i++) {
                $info = $result[$i];
                if ($i == (count($result) - 1)) {
                    $content .=  $info['row'] . "/" . $info['col'] . "/" . $info['status'] . "/" . $info['updatetime'];
                } else {
                    $content .=  $info['row'] . "/" . $info['col'] . "/" . $info['status'] . "/" . $info['updatetime'] . '</br>';
                }
                unset($info);
            }
        }
        if (empty($content)) {
            return  ['code' => -1, 'message' => '未查找到数据'];
        }
        return ['code' => 200, 'data' => $content];
    }
}
